package main

import (
	"fmt"
	"gin/CloudRestaurant/Middlewares"
	"gin/CloudRestaurant/controller"
	"gin/CloudRestaurant/tool"
	_ "github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
)

func main()  {
	cfg,err := tool.ParseConfig("./CloudRestaurant/config/app.json")
	if err != nil{
		panic(err.Error())
	}

	// 实例化数据库
	_,err = tool.OrmEngine(cfg)
	if err != nil{
		fmt.Println(err.Error())
		return
	}

	// 初始化redis配置
	tool.IntRedisStore()

	app := gin.Default()
	registerRouter(app)
	app.Use(Middlewares.Cors())
	app.Run(cfg.AppHost+":" + cfg.AppPort)
}

// 设置路由
func registerRouter(router *gin.Engine){
	// 登录与token
	new(controller.UserController).Router(router)
	// 其余api
	group := router.Group("v1")

	group.Use(Middlewares.JWTMiddleware())
	{
		new(controller.HelloController).Router(group)
		new(controller.MemberController).Router(group)
		new(controller.StudentController).Router(group)
	}

	//router.Use(Middlewares.JWT())

}
